package com.itboye.lottery.dialog;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.itboye.lottery.R;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.base.BaseDialog;
import com.itboye.lottery.bean.BalanceBean;
import com.itboye.lottery.bean.OrderBean;
import com.itboye.lottery.presenter.UserPresenter;
import com.itboye.lottery.retrofit.ResponseCallback;

import org.jetbrains.annotations.NotNull;

import butterknife.BindView;
import butterknife.OnClick;

public class FollowDialog extends BaseDialog implements ResponseCallback, TextWatcher {

    private  Activity activity;
    private UserPresenter userPresenter;
    @BindView(R.id.tv_lowest_money)
    TextView tv_lowest_money;
    @BindView(R.id.rg_follow)
    RadioGroup rg_follow;
    @BindView(R.id.tv_total_money)
    TextView tv_total_money;
    @BindView(R.id.tv_bottom_beishu_multiple)
    EditText tv_bottom_beishu_multiple;
    private int multiple = 5;
    private String id;
    private String lowAmount;
    private String singleAmount;

    public FollowDialog(Context context) {
        super(context);
    }

    private void initView() {
        setContentView(R.layout.dialog_follow);
        userPresenter = new UserPresenter(this);
        tv_bottom_beishu_multiple.addTextChangedListener(this);
    }

    public FollowDialog(Activity context, boolean fromButton) {
        super(context, fromButton);
        this.activity=context;
        initView();
    }


    @OnClick(R.id.iv_close)
    void close() {
        dismiss();
    }

    @OnClick(R.id.tv_follow)
    void sure() {
        userPresenter.followCase(Integer.valueOf(id), multiple);
    }

    @OnClick(R.id.iv_bottom_reduce_multiple)
    void reduce() {
        int value = Integer.parseInt(tv_bottom_beishu_multiple.getText().toString());
        value--;
        tv_bottom_beishu_multiple.setText(value + "");
//        initRadioButton();
        caculateTotalMoney(value);
    }

    @OnClick(R.id.iv_bottom_increase_multiple)
    void increase() {
        int value = Integer.parseInt(tv_bottom_beishu_multiple.getText().toString());
        value++;
        tv_bottom_beishu_multiple.setText(value + "");
//        initRadioButton();
        caculateTotalMoney(value);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tv_lowest_money.setText("最低跟单金额:" + lowAmount);
        tv_bottom_beishu_multiple.setText(this.multiple + "");
        initRadioButton();
        caculateTotalMoney(multiple);
    }

    private void initRadioButton() {
        for (int i = 0; i < rg_follow.getChildCount(); i++) {
            RadioButton radioButton = (RadioButton) rg_follow.getChildAt(i);
            if (Integer.parseInt(radioButton.getTag().toString()) == this.multiple) {
                radioButton.setChecked(true);
            }else{
                radioButton.setChecked(false);
            }
            radioButton.setOnCheckedChangeListener((compoundButton, b) -> {
                if (b) {
                    caculateTotalMoney(Integer.valueOf(compoundButton.getTag().toString()));
                    tv_bottom_beishu_multiple.setText("" + Integer.valueOf(compoundButton.getTag().toString()));
                } else {
//                    caculateTotalMoney(1);
                }
            });
        }
    }
//
//    @Override
//    protected void onStart() {
//        super.onStart();
//
//    }

    private void caculateTotalMoney(Integer integer) {
        this.multiple = integer;
        tv_total_money.setText("共" + integer * Double.parseDouble(singleAmount) + "元");
    }

    @Override
    public void success(@NotNull Object o, @NotNull String requestSuccessType) {
        if (requestSuccessType == userPresenter.paySupport_SUCCESS) {
        }
        if (requestSuccessType == userPresenter.getWithdrawalBalance_SUCCESS) {
            BalanceBean balanceBean = (BalanceBean) o;
        }
        if (requestSuccessType == userPresenter.pay_SUCCESS) {
            ByAlert.alert("支付成功");
        }
        if (requestSuccessType == userPresenter.followCase_SUCCESS) {
            dismiss();
            OrderBean orderBean = (OrderBean) o;
            PayDialog payDialog = new PayDialog(activity, true);
            payDialog.setOrderBean(orderBean);
            payDialog.show();
        }
    }

    @Override
    public void fail(int code, @NotNull Object msg, @NotNull String requestErrorType) {
        if (requestErrorType == userPresenter.paySupport_FAIL) {
            ByAlert.alert(msg);
        }
        if (requestErrorType == userPresenter.getWithdrawalBalance_FAIL) {
            ByAlert.alert(msg);
        }
        if (requestErrorType == userPresenter.pay_FAIL) {
            ByAlert.alert(msg);
        }
        if (requestErrorType == userPresenter.followCase_FAIL) {
            ByAlert.alert(msg);

        }
    }

    public void setEntity(String id, String lowAmount, String singleAmount) {
        this.id = id;
        this.lowAmount = lowAmount;
        this.singleAmount = singleAmount;
    }

    public void setMultiple(int multiple) {
        this.multiple = multiple;
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        System.out.println("开始计算---");
        String msg = editable.toString();
        if (TextUtils.isEmpty(msg)) {
            multiple = 0;
        } else {
            multiple = Integer.valueOf(msg);
        }
        caculateTotalMoney(multiple);
    }
}
